package LoginGUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginGUI implements ActionListener{
    private JFrame frame;
    private JLabel labelUsername, labelPassword, labelResult;
    private JTextField textFieldUserName;
    private JPasswordField passwordField;
    private JButton buttonLogin, buttonResset;

    LoginGUI(String title) {
        frame = new JFrame("title");
        frame.setSize(200,300);

        labelUsername = new JLabel("Username");
        labelPassword = new JLabel("Password");
        textFieldUserName = new JTextField(10);
        passwordField = new JPasswordField(10);
        buttonLogin = new JButton("login");
        buttonResset = new JButton("reset");
        labelResult = new JLabel("hahaha");




        //You must set a way of placing the items!
        frame.getContentPane().setLayout(new FlowLayout());
        frame.add(labelUsername);
        frame.add(textFieldUserName);
        frame.add(labelPassword);
        frame.add(passwordField);
        frame.add(buttonLogin);
        frame.add(buttonResset);
        frame.add(labelResult);

        //注册监听器对象
        buttonLogin.addActionListener(this);
        buttonResset.addActionListener(this);

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonLogin) {
            if (textFieldUserName.getText().equals("admin") && new String(passwordField.getPassword()).equals("123456")) {
                labelResult.setText("login success");
            } else {
                labelResult.setText("login failed");
            }
        } else if (e.getSource() == buttonResset) {
            textFieldUserName.setText("");
            passwordField.setText("");
        }
    }
}

//建议应用字符串比较的时候将字符串常量放在前面。
class LoginGUITest {
    public static void main(String[] args) {
        new LoginGUI("LoginGUI");
    }
}
